/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * NuGat-API named Chameleon developed by Simon De Baets to Verimag.
 *
 */


package chameleon.model.automata;

import chameleon.model.NodeTrans;
import chameleon.model.formula.Formula;
import chameleon.model.formula.Operator;
import chameleon.model.formula.Tag;
import java.util.Iterator;
import java.util.Vector;

/**
 * This class represent a Node to an automata.
 * @author simon
 */
public class Node {

    private String label_;
    private Vector<Edge> inputEdge_;
    private Vector<Edge> outputEdge_;

    public Node (String label){
        label_ = label;
        inputEdge_ = new Vector<Edge>();
        outputEdge_ = new Vector<Edge>();
    }

    private Node (Node node){
        label_ = node.getLabel();
        inputEdge_ = (Vector<Edge>) node.getInputEdge().clone();
        outputEdge_ = (Vector<Edge>) node.getOutputEdge().clone();
    }

    public Node clone(){
        return new Node(this);
    }

    public String getLabel(){
        return label_;
    }

    public Vector<Edge> getInputEdge(){
        return inputEdge_;
    }

    public Vector<Edge> getOutputEdge(){
        return outputEdge_;
    }

    public void addInputEdge(Edge input){
        inputEdge_.add(input);
    }

    public void addOutputEdge(Edge output){
        outputEdge_.add(output);
    }

    public Vector<NodeTrans> getNuGatTransition(){
        Vector<NodeTrans> transRelation = new Vector<NodeTrans>();

        Formula targetFormula = getOutputTransition();

        Iterator iter = inputEdge_.iterator();

        while (iter.hasNext()){
            Edge edge = (Edge) iter.next();
            NodeTrans trans =  new NodeTrans(edge.getLabel(), targetFormula);
            transRelation.add(trans.clone());
        }

        return transRelation;
    }

    private Formula getOutputTransition() {
            Formula outputTransition;
            Formula nextFormula;
            Vector<Formula> targetFormula = new Vector<Formula>();
            Iterator iter = outputEdge_.iterator();

            //store all the output label
            while (iter.hasNext()) {
                Edge edge = (Edge) iter.next();
                Formula formula = edge.getLabel();

                if (isNotPresent(targetFormula, formula))
                    targetFormula.add(formula.clone());
            }

            //creation of the output formula
            iter = targetFormula.iterator();
            outputTransition = (Formula) iter.next();
            if (iter.hasNext()){
                nextFormula = new Formula(outputTransition.clone(), new Operator(Tag.S_NEXT_TRANSITION));
                outputTransition = nextFormula.clone();
            }

            while (iter.hasNext()) {
                Formula newFormula = (Formula) iter.next();
                nextFormula = new Formula(newFormula.clone(), new Operator(Tag.S_NEXT_TRANSITION));
                outputTransition = new Formula (outputTransition.clone(),
                        nextFormula.clone(), new Operator(Tag.S_OR));
            }
            return outputTransition;

    }

    private boolean isNotPresent(Vector<Formula> targetFormula, Formula formula) {
        Iterator iter = targetFormula.iterator();

        while (iter.hasNext()){
            Formula currentFormula = (Formula) iter.next();
                if (currentFormula.isEquals(formula)) {
                    return false;
                }
            
        }

        return true;
    }

}
